package com.example.network.controller;

import com.example.network.common.Result;
import com.example.network.controller.request.LoginRequest;
import com.example.network.controller.request.PassRequest;
import com.example.network.controller.request.UserDataRequest;
import com.example.network.model.entity.User;
import com.example.network.model.entity.UserData;
import com.example.network.service.EmailService;
import com.example.network.service.TimeService;
import com.example.network.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author LR
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Tag(name = "user接口 -黎")
public class UserController{
    @Resource
    private UserService userService;
    @Resource
    private TimeService timeService;
    @Resource
    private EmailService emailService;

    /**
     * 用户登录
     */
    @PostMapping("login")
    @Operation(summary = "用户登录")
    public Result login(@RequestBody LoginRequest request){
        log.info("用户{}登录",request.getEmail());
        timeService.insertTime(request.getEmail(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"登录");
        return Result.success(userService.login(request));
    }
    /**
     * 新增用户
     */
    @PostMapping("/insert")
    @Operation(summary = "新增用户")
    public Result insert(@RequestBody User user) {
        userService.insert(user);
        log.info("新增用户{}",user.getUsername());
        timeService.insertTime(user.getEmail(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"注册");
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/update")
    @Operation(summary = "修改密码")
    public Result update(@RequestBody PassRequest request) {
        userService.updateByUsername(request);
        log.info("用户{}修改密码",request.getEmail());
        timeService.insertTime(request.getEmail(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"修改密码");
        return Result.success();
    }

    /**
     * 删除一条数据
     */
    @DeleteMapping("/del/{email}")
    @Operation(summary = "注销用户")
    public Result delete(@PathVariable String email) {
        userService.DeleteByUserEmail(email);
        timeService.insertTime(email, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"用户被注销");
        return Result.success();
    }

    /**
     * 发送验证码
     */
    @GetMapping("/sendCode")
    @Operation(summary = "发送验证码")
    public Result sendCode(@RequestParam String email){
        log.info("向{}邮箱用户发送验证码",email);
        emailService.sendCode(email);
        return Result.success();
    }

    /**
     * 修改用户资料
     */
    @PutMapping("/midifyData")
    @Operation(summary = "修改用户资料")
    public Result modifyData(@RequestBody UserDataRequest userData){
        log.info("用户{}修改了资料",userData.getEmail());
        timeService.insertTime(userData.getEmail(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"修改资料");
        userService.modifyData(userData);
        return Result.success();
    }

    /**
     * 修改头像
     */
    @PutMapping("/setAvatars")
    @Operation(summary = "修改用户头像")
    public Result setAvatars(@RequestParam("avatars") MultipartFile file, @RequestParam("email") String email){
        log.info("用户{}修改了头像",email);
        timeService.insertTime(email, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"上传头像");
        userService.setAvatars(file,email);
        return Result.success();
    }

    @GetMapping("/getImage")
    @Operation(summary = "显示用户头像")
    public ResponseEntity<InputStreamResource> getImage(@RequestParam String email){
        log.info("显示用户{}的头像",email);
        return userService.getImage(email);
    }
    /**
     * 查看用户信息
     */
    @GetMapping("/getUserData")
    @Operation(summary = "查看用户资料")
    public Result getUserData(@RequestParam String email){
        log.info("用户{}查看了资料",email);
        UserData userData = userService.getUserData(email);
        return Result.success(userData);
    }
}
